package com.payment.ktb.activity.main1;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.ConstantsPay;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.dialog.CardsDialog;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.Md5Utils;
import com.payment.ktb.utils.SharedPreferencesUtils;
import com.payment.ktb.utils.StringUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/6/9.
 */
public class NocardOrderConfirmActivity extends BaseActivity implements CardsDialog.OnCardDialogClickListener {

    @BindView(R.id.tv_nocardorderconfirm_amount)
    TextView tv_nocardorderconfirm_amount;
    @BindView(R.id.tv_nocardorderconfirm_time)
    TextView tv_nocardorderconfirm_time;
    @BindView(R.id.tv_nocardorderconfirm_orderno)
    TextView tv_nocardorderconfirm_orderno;
    @BindView(R.id.tv_nocardorderconfirm_bankcardno)
    TextView tv_nocardorderconfirm_bankcardno;
    @BindView(R.id.tv_nocardorderconfirm_bankcardName)
    TextView tv_nocardorderconfirm_bankcardName;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nocardorderconfirm);
        ButterKnife.bind(this);
        initTitle("确认订单");
        initData();
    }


    /**
     *
     */
    private void initData() {
        tv_nocardorderconfirm_amount.setText(ConstantsPay.amount + "元");
        tv_nocardorderconfirm_time.setText(ConstantsPay.orderTime);
        tv_nocardorderconfirm_orderno.setText(ConstantsPay.orderNo);
        if (ConstantsPay.bindCards != null && ConstantsPay.bindCards.size() > 0) {
            ConstantsPay.bankNo = ConstantsPay.bindCards.get(0).getBankNo();
            tv_nocardorderconfirm_bankcardName.setText(ConstantsPay.bindCards.get(0).getBankName());
            tv_nocardorderconfirm_bankcardno.setText(StringUtils.handleCard(ConstantsPay.bankNo));
        }
    }


    @OnClick({R.id.btn_nocardorderconfirm_next,R.id.rl_nocardorderconfirm_selcetbankcard,R.id.ll_nocardorderconfirm_cardno})
    public void clickEvent(View view) {

        switch (view.getId()) {

            //支付银行卡
            case R.id.ll_nocardorderconfirm_cardno:
                new CardsDialog(mContext, ConstantsPay.bindCards,this);
//                startActivity(AddBankcardActivity.class);
                break;

            //支付银行卡
            case R.id.rl_nocardorderconfirm_selcetbankcard:
                new CardsDialog(mContext, ConstantsPay.bindCards,this);
                break;

            //下一步
            case R.id.btn_nocardorderconfirm_next:

                if (TextUtils.isEmpty(ConstantsPay.bankNo)) {
                    ToastUtils.showToast("请输入交易银行卡卡号！");
                } else {
                    next();
                }

                break;

        }


    }




    /**
     *
     */
    private void next() {

        progressDialogUtils.showProgress();

        //构造传输参数
        String orderNo = ConstantsPay.orderNo;
        String bankNo = ConstantsPay.bankNo;
        String token = SharedPreferencesUtils.getString(ConstantsUser.userToken);
        String sign = Md5Utils.md5Hex(bankNo +orderNo + SharedPreferencesUtils.getString(ConstantsUser.userKey));



        //将参数放入map中
        HashMap<String, String> map = new HashMap<String,String>();

        map.put("orderNo",orderNo);
        map.put("bankNo",bankNo);
        map.put("token",token);
        map.put("sign",sign);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.nocardOrderBankState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                progressDialogUtils.dismissProgress();

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.optString("code");
                    if (Constants.NORMAL_CODE.equals(code)) {
                        ConstantsPay.selfFlg = jsonObject.optString("selfFlg");
                        ConstantsPay.bankCardType = jsonObject.optString("bankCardType");
                        ConstantsPay.bankCardName = jsonObject.optString("bankCardName");
                        ConstantsPay.bankCardIcon = jsonObject.optString("bankCardIcon");
                        ConstantsPay.bankPhone = jsonObject.optString("bankPhone");
                        if ("1".equals(ConstantsPay.selfFlg)) {
                            startActivity(NocardOrderSubmitBindActivity.class);
                        } else {
                            startActivity(NocardOrderSubmitNobindActivity.class);
                        }


                    } else {

                        AlertDialogUtils.showAlertDialog(mContext, jsonObject.optString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {

                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.fail_to_connect_server));
            }
        });


    }

    @Override
    public void onCardDialogItemClickListener(String cardNo, String cardBank) {
        ConstantsPay.bankNo = cardNo;
        tv_nocardorderconfirm_bankcardno.setText(StringUtils.handleCard(cardNo));
        tv_nocardorderconfirm_bankcardName.setText(cardBank);
    }
}
